package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.CompanyInfoMapper;
import com.suyuan.dao.PigOutMapper;
import com.suyuan.dao.ProducetypeInfoMapper;
import com.suyuan.dao.ProductInfoMapper;
import com.suyuan.entity.*;
import com.suyuan.service.ButcherProductService;
import com.suyuan.utils.DeleteRows;
import com.suyuan.vo.ProductInfoVO;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 屠宰包装控制类
 */

@Service("butcherProductServiceImpl")
public class ButcherProductServiceImpl implements ButcherProductService {


    @Resource
    private ProductInfoMapper productInfoMapper;
    @Resource
    private CompanyInfoMapper companyInfoMapper;
    @Resource
    private ProducetypeInfoMapper producetypeInfoMapper;
    @Resource
    private PigOutMapper pigOutMapper;

    /**
     * 多条件查询
     * @param productID
     * @param pigID
     * @param compID
     * @param typeID
     * @param page
     * @return
     */
    public PageInfo findByCondition(Integer productID, Integer pigID, Integer compID, Integer typeID, Page page) {
        ProductInfoExample productInfoExample = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = productInfoExample.createCriteria();
        if(productID != null){
            criteria.andProductidEqualTo(productID);
        }
        if(pigID != null){
            criteria.andAnimalidEqualTo(pigID);
        }
        if(compID != null){
            criteria.andCompidEqualTo(compID);
        }
        if(typeID != null){
            criteria.andTypeidEqualTo(typeID);
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List productInfoList = productInfoMapper.selectByExample(productInfoExample);
        PageInfo pageInfo = new PageInfo(productInfoList);
        List<ProductInfoVO> productInfoVOList = new ArrayList<ProductInfoVO>();
        for(ProductInfo productInfo: (List<ProductInfo>)pageInfo.getList()){
            CompanyInfo companyInfo = companyInfoMapper.selectByPrimaryKey(productInfo.getCompid());
            ProducetypeInfo producetypeInfo = producetypeInfoMapper.selectByPrimaryKey(productInfo.getTypeid());
            productInfoVOList.add(new ProductInfoVO(productInfo,companyInfo,producetypeInfo,null));
        }
        pageInfo.setList(productInfoVOList);
        return pageInfo;
    }

    //单项删除
    public Integer deleteInfo(Integer id) {
        productInfoMapper.deleteByPrimaryKey(id);
        return 1;
    }

    //批量删除
    @Transactional
    public Integer deleteInfos(String ids) {
        DeleteRows deleteRows = new DeleteRows();
        int [] deleteID = deleteRows.selectMore(ids);
        for (Integer ID : deleteID){
            productInfoMapper.deleteByPrimaryKey(ID);
        }
        return 1;
    }

    public ProductInfo findByProductID(Integer gid) {
        return   productInfoMapper.selectByPrimaryKey(gid);
    }

    public List<CompanyInfo> findAllCompany() {
        return companyInfoMapper.selectByExample(null);
    }

    public List<ProducetypeInfo> findAllProduce() {
        return producetypeInfoMapper.selectByExample(null);
    }

    /**
     * 更新屠宰包装信息
     * @param productInfo
     */
    public void updateInfo(ProductInfo productInfo) {
        ProductInfo productInfo1 = productInfoMapper.selectByPrimaryKey(productInfo.getProductid());
        productInfo1.setCompid(productInfo.getCompid());
        productInfo1.setInspectercode(productInfo.getInspectercode());
        productInfo1.setInspectresult(productInfo.getInspectresult());
        productInfo1.setInspecttime(productInfo.getInspecttime());
        productInfo1.setProcesstime(productInfo.getProcesstime());
        productInfo1.setSalerid(productInfo.getSalerid());
        productInfo1.setTypeid(productInfo.getTypeid());
        productInfo1.setTracecode(productInfo.getTracecode());
        productInfoMapper.updateByPrimaryKeySelective(productInfo1);
    }

    /**
     * 添加生猪信息
     * @param productInfo
     */
    public void addProductInfo(ProductInfo productInfo) {
        productInfoMapper.insert(productInfo);
    }

    /**
     * 检验生猪编号
     * @param id
     * @return
     */
    public boolean checkPurcharse(Integer id) {
        PigOutExample pigOutExample = new PigOutExample();
        PigOutExample.Criteria criteria = pigOutExample.createCriteria();
        criteria.andPurGidEqualTo(id);
        List<PigOut> pigOutList = pigOutMapper.selectByExample(pigOutExample);
        if(pigOutList.size() > 0){
            return true;
        }
        return false;
    }
}
